/**
 * 27. 移除元素
 * https://leetcode-cn.com/problems/remove-element/
 */
public class Solutions_27 {
    public static void main(String[] args) {
        int[] nums = {3, 2, 2, 3};
        int val = 3;  // output: 2,  nums = {2, 2}

//        int[] nums = {0, 1, 2, 2, 3, 0, 4, 2};
//        int val = 2;  // output: 5,  nums = {0, 1, 4, 0, 3}

        int result = removeElement(nums, val);
        System.out.println(result);
    }

    /**
     * 解题思路：双指针（快慢指针）
     * 快指针遍历元素，慢指针记录元素
     */
    public static int removeElement(int[] nums, int val) {
        int slow = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[slow] = nums[i];
                slow ++;
            }
        }
//        System.out.println(Arrays.toString(nums));
        return slow;
    }
}
